package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    RoomFacilityService roomFacilityService;

    @Autowired
    RoomLabelService roomLabelService;

    @Autowired
    RoomAttrValueService roomAttrValueService;

    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    RoomFacilityMapper roomFacilityMapper;

    @Autowired
    RoomAttrValueMapper roomAttrValueMapper;

    @Autowired
    RoomLabelMapper roomLabelMapper;

    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {

        // 判断更新还是新增
        Long id = roomSubmitVo.getId();

        // 如果是更新删除6张表信息
        if(id!=null){

            // 删除图片
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);// 2
            graphInfoQueryWrapper.eq("item_id",id);
            graphInfoService.remove(graphInfoQueryWrapper);
            // 删除标签中间表
            QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
            roomLabelQueryWrapper.eq("room_id",id);
            roomLabelService.remove(roomLabelQueryWrapper);
            // 删除配套中间表
            QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
            roomFacilityQueryWrapper.eq("room_id",id);
            roomLabelService.remove(roomLabelQueryWrapper);
            // 删除属性中间表
            QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
            roomAttrValueQueryWrapper.eq("room_id",id);
            roomAttrValueService.remove(roomAttrValueQueryWrapper);
            // 删除租期中间表
            QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
            roomLeaseTermQueryWrapper.eq("room_id",id);
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);
            // 删除支付方式中间表
            QueryWrapper<RoomPaymentType> roomPaymentTypeServiceQueryWrapper = new QueryWrapper<>();
            roomPaymentTypeServiceQueryWrapper.eq("room_id",id);
            roomPaymentTypeService.remove(roomPaymentTypeServiceQueryWrapper);
        }

        // 新增房间信息
        this.saveOrUpdate(roomSubmitVo);

        // 新增其他6张表信息
        Long roomId = roomSubmitVo.getId();

        //1.保存新的graphInfoList
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //2.保存新的roomAttrValueList
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        //3.保存新的facilityInfoList
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }

        //4.保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        //5.保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        //6.保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);

        }
    }

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomInfo> roomInfoIPage, RoomQueryVo roomQueryVo) {

        IPage<RoomItemVo> itemVoIPage = baseMapper.selectPageItem(roomInfoIPage,roomQueryVo);

        return itemVoIPage;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {

//        1 房间信息
        RoomInfo roomInfo = baseMapper.selectById(id);
//        2 图片信息
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type",2);
        graphInfoQueryWrapper.eq("item_id",id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoQueryWrapper);
//        3 房间标签
        List<LabelInfo> labelInfos = roomLabelMapper.selectListByRoomId(id);
//        4 房间属性
        List<AttrValueVo> attrValueVos = roomAttrValueMapper.selectListByRoomId(id);

//        5 房间配套
        List<FacilityInfo> facilityInfos = roomFacilityMapper.selectListByRoomId(id);

//        6 房间租期
        List<LeaseTerm> leaseTerms = roomLeaseTermMapper.selectListByRoomId(id);

//        7 房间支付方式
        List<PaymentType> paymentTypes = roomPaymentTypeMapper.selectListByRoomId(id);
//        8 公寓信息
        QueryWrapper<ApartmentInfo> apartmentInfoQueryWrapper = new QueryWrapper<>();
        apartmentInfoQueryWrapper.eq("id",roomInfo.getApartmentId());
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(apartmentInfoQueryWrapper);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setLeaseTermList(leaseTerms);
        roomDetailVo.setLabelInfoList(labelInfos);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setAttrValueVoList(attrValueVos);
        List<GraphVo> graphVos = graphInfos.stream().map(graphInfo->{
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            return graphVo;
        }).collect(Collectors.toList());
        roomDetailVo.setGraphVoList(graphVos);

        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {

        // 房间配套
        QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
        roomFacilityQueryWrapper.eq("room_id",id);
        roomFacilityMapper.delete(roomFacilityQueryWrapper);

        // 房间属性
        QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
        roomAttrValueQueryWrapper.eq("room_id",id);
        roomAttrValueMapper.delete(roomAttrValueQueryWrapper);

        // 房间标签
        QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
        roomLabelQueryWrapper.eq("room_id",id);
        roomLabelMapper.delete(roomLabelQueryWrapper);

        // 房间租期
        QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
        roomLeaseTermQueryWrapper.eq("room_id",id);
        roomLeaseTermMapper.delete(roomLeaseTermQueryWrapper);

        // 房间支付
        QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq("room_id",id);
        roomPaymentTypeMapper.delete(roomPaymentTypeQueryWrapper);

        // 图片
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type",2);
        graphInfoQueryWrapper.eq("item_id",id);
        graphInfoMapper.delete(graphInfoQueryWrapper);

        // 房间数据
        baseMapper.deleteById(id);

    }
}




